﻿package com.designoutput.ui.newControls
{
    import com.designoutput.event.MessageBoxEvent;
    import com.designoutput.ui.controls.Button;
    import com.designoutput.ui.controls.Literal;
    
    import flash.display.DisplayObject;
    import flash.events.*;
    import com.designoutput.manager.ResourceManager;

	[Event(name="okYes", type="com.designoutput.event.MessageBoxEvent")]
	[Event(name="cancel", type="com.designoutput.event.MessageBoxEvent")]
	[Event(name="no", type="com.designoutput.event.MessageBoxEvent")]
    public class MessageBox extends DialogBox
    {
		public static const OK:String = "Ok";
		public static const OK_CANCEL:String = "OkCancel";
		public static const YES_NO:String = "YesNo";
		public static const YES_NO_CANCEL:String = "YesNoCancel";
		
        private var cancelButton:Button;
        private var okButton:Button;
		private var yesButton:Button;
		private var noButton:Button;
		private var messageLiteral:Literal;

		private var _visibleButtons:String = "Ok";
		
		public function get visibleButtons():String {
			return _visibleButtons;
		}
		public function set visibleButtons(value:String):void {
			_visibleButtons = value;
			invalidateDisplay();
		}
		
		override protected function initialize():void {
			super.initialize();
			
			width = 200;
			height = 150;

			this.setTitle(ResourceManager.getValue("title"));
			
			messageLiteral = new Literal();
			addChild(messageLiteral);
			
			okButton = new Button(ResourceManager.getValue("rm_ok"), 70);
			okButton.addEventListener(MouseEvent.CLICK, this.okYesClick, false, 0, true);
			addChild(okButton);
			
			cancelButton = new Button(ResourceManager.getValue("rm_cancel"), 70);
			cancelButton.addEventListener(MouseEvent.CLICK, this.cancelClick, false, 0, true);
			addChild(cancelButton);		
			
			yesButton = new Button(ResourceManager.getValue("rm_yes"), 70);
			yesButton.addEventListener(MouseEvent.CLICK, this.okYesClick, false, 0, true);
			addChild(yesButton);
			
			noButton = new Button(ResourceManager.getValue("rm_no"), 70);
			noButton.addEventListener(MouseEvent.CLICK, this.noClick, false, 0, true);
			addChild(noButton);
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			messageLiteral.width = _width - 10;
			messageLiteral.x = _width / 2 - messageLiteral.width / 2;
			messageLiteral.y = 40;
			messageLiteral.height = _height - 60;
			
			okButton.visible = cancelButton.visible = yesButton.visible = noButton.visible = false;
			
			//Layout buttons horizontaly
			var buttons:Array = [];
			
			switch(visibleButtons){
				case OK :
					buttons.push(okButton);
					break;
				case OK_CANCEL :
					buttons.push(okButton, cancelButton);
					break;
				case YES_NO :
					buttons.push(yesButton, noButton);
					break;
				case YES_NO_CANCEL :
					buttons.push(yesButton, noButton, cancelButton);
			}
			
			var buttonsWidth:Number = 0;
			for each(var button:DisplayObject in buttons)
				buttonsWidth += button.width;
			
			var gap:Number = 5;
			
			var totalButtonsWidth:Number = buttonsWidth + (buttons.length-1) * gap;
			
			var currX:Number = _width/2 - totalButtonsWidth/2;
			
			for each(var b:DisplayObject in buttons){
				b.visible = true;
				b.x = currX;
				b.y = _height - 50;
				currX += b.width + gap;
			}
		}
		
		public function setMessage(message:String):void {
			messageLiteral.text = message;
		}

        protected function okYesClick(event:MouseEvent) : void
        {
			dispatchEvent( new MessageBoxEvent( MessageBoxEvent.OK_YES ));
            close();
        }
		
		protected function noClick(event:MouseEvent) : void
		{
			dispatchEvent( new MessageBoxEvent( MessageBoxEvent.NO ));
			close();
		}

        protected function cancelClick(event:MouseEvent) : void
        {
			onCloseClick();
        }
		
		override protected function onCloseClick():void {
			dispatchEvent( new MessageBoxEvent( MessageBoxEvent.CANCEL ));
			close();
		}
		
		override protected function onConfirmKey():void {
			okYesClick(null);
		}
		
		override protected function onCancelKey():void {
			onCloseClick();
		}
    }
}
